<?php
session_start();

if ( $_SESSION['manager'] )
{
    require_once('../inc/config.db.php');

    switch ( $_GET['dictionary'] )
    {
	case	'managers':
	    $result = mysql_query(sprintf('SELECT * FROM `manager` ORDER BY `manager_name`'));
	    while ( $row = mysql_fetch_assoc($result) )
	    {
		$manager[] = $row;
	    }
	    print json_encode($manager);
	    break;

	case	'groups':
	    $result = mysql_query(sprintf('SELECT * FROM `group` ORDER BY `group_name`'));
	    while ( $row = mysql_fetch_assoc($result) )
	    {
		$group[] = $row;
	    }
	    print json_encode($group);
	    break;

	case	'statuses':
	    $result = mysql_query(sprintf('SELECT * FROM `status` ORDER BY `status_name`'));
	    while ( $row = mysql_fetch_assoc($result) )
	    {
		$status[] = $row;
	    }
	    print json_encode($status);
	    break;

	case	'posts':
	    $result = mysql_query(sprintf('SELECT DISTINCT `post` FROM `contact` WHERE `post` LIKE \'%%%s%%\' ORDER BY `post`',$_GET['term']));
	    while ( $row = mysql_fetch_array($result) )
	    {
		$post[] = $row[0];
	    }
	    print json_encode($post);
	    break;

	case	'regions':
	    $result = mysql_query(sprintf('SELECT DISTINCT `legal_region` FROM `customer` WHERE `legal_region` LIKE \'%%%s%%\' ORDER BY `legal_region`',$_GET['term']));
	    while ( $row = mysql_fetch_array($result) )
	    {
		$array[] = $row[0];
	    }
	    $result = mysql_query(sprintf('SELECT DISTINCT `actual_region` FROM `customer` WHERE `actual_region` LIKE \'%%%s%%\' ORDER BY `actual_region`',$_GET['term']));
	    while ( $row = mysql_fetch_array($result) )
	    {
		$array[] = $row[0];
	    }
	    $result = mysql_query(sprintf('SELECT DISTINCT `postal_region` FROM `customer` WHERE `postal_region` LIKE \'%%%s%%\' ORDER BY `postal_region`',$_GET['term']));
	    while ( $row = mysql_fetch_array($result) )
	    {
		$array[] = $row[0];
	    }

	    print json_encode(array_unique($array));
	    break;

	case	'cities':
	    $result = mysql_query(sprintf('SELECT DISTINCT `legal_city` FROM `customer` WHERE `legal_city` LIKE \'%%%s%%\' ORDER BY `legal_city`',$_GET['term']));
	    while ( $row = mysql_fetch_array($result) )
	    {
		$array[] = $row[0];
	    }
	    $result = mysql_query(sprintf('SELECT DISTINCT `actual_city` FROM `customer` WHERE `actual_city` LIKE \'%%%s%%\' ORDER BY `actual_city`',$_GET['term']));
	    while ( $row = mysql_fetch_array($result) )
	    {
		$array[] = $row[0];
	    }
	    $result = mysql_query(sprintf('SELECT DISTINCT `postal_city` FROM `customer` WHERE `postal_city` LIKE \'%%%s%%\' ORDER BY `postal_city`',$_GET['term']));
	    while ( $row = mysql_fetch_array($result) )
	    {
		$array[] = $row[0];
	    }

	    print json_encode(array_unique($array));
	    break;

	case	'streets':
	    $result = mysql_query(sprintf('SELECT DISTINCT `legal_street` FROM `customer` WHERE `legal_street` LIKE \'%%%s%%\' ORDER BY `legal_street`',$_GET['term']));
	    while ( $row = mysql_fetch_array($result) )
	    {
		$array[] = $row[0];
	    }
	    $result = mysql_query(sprintf('SELECT DISTINCT `actual_street` FROM `customer` WHERE `actual_street` LIKE \'%%%s%%\' ORDER BY `actual_street`',$_GET['term']));
	    while ( $row = mysql_fetch_array($result) )
	    {
		$array[] = $row[0];
	    }
	    $result = mysql_query(sprintf('SELECT DISTINCT `postal_street` FROM `customer` WHERE `postal_street` LIKE \'%%%s%%\' ORDER BY `postal_street`',$_GET['term']));
	    while ( $row = mysql_fetch_array($result) )
	    {
		$array[] = $row[0];
	    }

	    print json_encode(array_unique($array));
	    break;

	case	'customers':
	    if ( $_GET['show_deleted'] == 1 )
		if ( $_GET['only_deleted'] == 1 )
		    $filter = 'AND `is_deleted` IS NOT NULL';
		else
		    $filter = '';
	    else
		$filter = 'AND `is_deleted` IS NULL';

	    if ( $_SESSION['manager']['is_admin'] )
		$result = mysql_query(sprintf('SELECT `customer_name` FROM `customer` WHERE `customer_name` LIKE \'%%%1$s%%\' %2$s ORDER BY `customer_name` LIMIT 15',mysql_escape_string($_GET['term']),$filter));

	    else
		$result = mysql_query(sprintf('SELECT `customer_name` FROM `customer` WHERE `customer_name` LIKE \'%%%1$s%%\' AND `manager_id`=%2$d %3$s ORDER BY `customer_name` LIMIT 15',mysql_escape_string($_GET['term']),$_SESSION['manager']['id'],$filter));

	    if ( $result )
		while ( $row = mysql_fetch_array($result) )
		    $customer[] = stripslashes($row[0]);
	    else die(mysql_error());

	    print json_encode($customer);
	    break;


   	case	'searchphonenumbers':
	    
		$result = mysql_query(sprintf('SELECT `phone_number` from `service_phone_number` where `service_id` is NULL and phone_number LIKE \'%%%1$d%%\' limit 15',mysql_escape_string($_GET['term'])));

	    
		while ( $row = mysql_fetch_array($result) )
		    $searchphonenumbers[] = stripslashes($row[0]);
	  

	    print json_encode($searchphonenumbers);
	    break;

	case	'searchphonenumbersmain':
	    
		$result = mysql_query(sprintf('SELECT `phone_number` from `service_phone_number` where `service_id` is NOT NULL and own_number=1 and phone_number LIKE \'%%%1$d%%\' limit 15',mysql_escape_string($_GET['term'])));

	    
		while ( $row = mysql_fetch_array($result) )
		    $searchphonenumbers[] = stripslashes($row[0]);
	  

	    print json_encode($searchphonenumbers);
	    break;

	case	'searchdevicesn':
	    
		$result = mysql_query(sprintf('SELECT `SN` from `voip_devices_register` where SN like \'%%%1$s%%\' LIMIT 100',mysql_escape_string($_GET['term'])));

	    
		while ( $row = mysql_fetch_array($result) )
		    $searchdevicesn[] = stripslashes($row[0]);
	  

	    print json_encode($searchdevicesn);
	    break;

    }
}
?>
